package com.itheima.controller.backend.security;

import com.itheima.Service.SetmealService;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 图片上传
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile imgFile) throws IOException {
        log.info("图片上传:{}",imgFile);
        String imgUrl= setmealService.upload(imgFile);
        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,imgUrl);
        return new Result(true,"图片上传成功",imgUrl);
    }
    /**
     * 套餐添加
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    public Result add(String checkgroupIds,@RequestBody Setmeal setmeal){
        log.info("套餐添加:{}++{}",checkgroupIds,setmeal);
        setmealService.add(checkgroupIds,setmeal);

        return new Result(true,"套餐添加成功",null);
    }
    /**
     * 编辑套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/edit")
    public Result update(@RequestBody Setmeal setmeal,String checkgroupIds) throws Exception {
        log.info("编辑套餐:{}++{}",setmeal,checkgroupIds);
        setmealService.update(setmeal,checkgroupIds);
        return new Result(true,"编辑套餐",null);
    }
    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(String id){
        log.info("根据id查询套餐:{}",id);
        Setmeal setmeal= setmealService.findById(id);
        return new Result(true,"根据id查询套餐",setmeal);
    }
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}",queryPageBean);
        PageResult pageResult= setmealService.findPage(queryPageBean);
        return pageResult;
    }

}
